package com.shouer.doc.widget;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathEffect;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;

import com.shouer.doc.R;

/**
 * Created by apple on 16/6/11.
 */
public class DashLine extends View{

    private final int DEFAULT_LINE_COLOR=0X000000;

    private int DEFAULT_WIDTH=40;

    private Paint mPaint;
    private int dashLineColor=DEFAULT_LINE_COLOR;
    private int dashGap=DEFAULT_WIDTH;
    private int dashWidth=DEFAULT_WIDTH;
    private int stockWidth;
    private int vCenter;

    public DashLine(Context context) {
        super(context);
    }

    public DashLine(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context,attrs);
    }

    public DashLine(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context,attrs);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public DashLine(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context,attrs);
    }

    private void init(Context context,AttributeSet attrs)
    {
        TypedArray styledAttrs = context.obtainStyledAttributes(attrs, R.styleable.DashLine);
        dashLineColor=styledAttrs.getColor(R.styleable.DashLine_line_color,DEFAULT_LINE_COLOR);
        dashGap=styledAttrs.getDimensionPixelSize(R.styleable.DashLine_dash_gap,DEFAULT_WIDTH);
        dashWidth=styledAttrs.getDimensionPixelSize(R.styleable.DashLine_dash_width,DEFAULT_WIDTH);
        styledAttrs.recycle();
    }


    @Override
    public void onDraw(Canvas canvas)
    {
        super.onDraw(canvas);
        Path path = new Path();
        path.moveTo(0, vCenter);
        path.lineTo(getWidth(),vCenter);
        PathEffect effects = new DashPathEffect(new float[]{dashWidth,dashGap},1);
        mPaint.setPathEffect(effects);
        canvas.drawPath(path, mPaint);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {

        super.onSizeChanged(w,h,oldw,oldh);
        stockWidth=h;
        vCenter=h/2;

        mPaint=new Paint();
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(dashLineColor);
        mPaint.setStrokeWidth(stockWidth);

    }

}
